import { Module } from '@nestjs/common';
import { TypeOrmModule, type TypeOrmModuleOptions } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { RedisModule } from './redis/redis.module';
import { EmailModule } from './email/email.module';
import { APP_GUARD } from '@nestjs/core';
import { LoginGuard } from './login.guard';
import { PermissionGuard } from './permission.guard';

import { JwtModule } from '@nestjs/jwt';
import { UserModule } from './user/user.module';
import { GoodsModule } from './goods/goods.module';
import { OrderModule } from './order/order.module';
import { AddressModule } from './address/address.module';
import { MessageModule } from './message/message.module';
import { FavoriteModule } from './favorite/favorite.module';
import { PermissionModule } from './permission/permission.module';
import { User } from './user/entities/user.entity';
import { Role } from './user/entities/role.entity';
import { Permission } from './user/entities/permission.entity';
import { Goods } from './goods/entities/goods.entity';
import { Category } from './goods/entities/category.entity';
import { Comment } from './goods/entities/comment.entity';
import { Order } from './order/entities/order.entity';
import { Address } from './address/entities/address.entity';
import { Message } from './message/entities/message.entity';
import { Favorite } from './favorite/entities/favorite.entity';
import { Community } from './community/entities/community.entity';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { CommunityModule } from './community/community.module';
import { MinioModule } from './minio/minio.module';
import { Log } from './log/entities/log.entity';
import { LogModule } from './log/log.module';

@Module({
  imports: [
    // 配置模块
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: 'src/.env',
    }),
    // TypeORM配置
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService): TypeOrmModuleOptions => ({
        type: 'mysql' as const,
        host: configService.get<string>('mysql_server_host'),
        port: configService.get<number>('mysql_server_port'),
        username: configService.get<string>('mysql_server_username'),
        password: configService.get<string>('mysql_server_password'),
        database: configService.get<string>('mysql_server_database'),
        entities: [
          User,
          Role,
          Permission,
          Goods,
          Category,
          Comment,
          Order,
          Address,
          Message,
          Favorite,
          Community,
        ],
        synchronize: true,
        logging: true,
        poolSize: 10,
        connectorPackage: 'mysql2',
        extra: {
          authPlugin: 'sha256_password',
        },
      }),
    }),
    // JWT配置
    JwtModule.registerAsync({
      global: true,
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => ({
        secret: configService.get('jwt_secret'),
        signOptions: {
          expiresIn: configService.get('jwt_access_token_expires_time'),
        },
      }),
    }),
    // 业务模块
    UserModule,
    GoodsModule,
    OrderModule,
    AddressModule,
    MessageModule,
    FavoriteModule,
    RedisModule,
    EmailModule,
    CommunityModule,
    PermissionModule,
    MinioModule,
    LogModule,
  ],
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: APP_GUARD,
      useClass: LoginGuard,
    },
    {
      provide: APP_GUARD,
      useClass: PermissionGuard,
    },
  ],
})
export class AppModule {}
